package algorithm.leetcode.simple;

/*
 将一个按照升序排列的有序数组，转换为一棵高度平衡二叉搜索树。
 */

/**
 * @author jack.wu
 * @version 1.0
 * @date 2020/10/12
 */
public class Question104 {

    public static void main(String[] args) {

    }

    public TreeNode sortedArrayToBST(int[] nums) {
        if (nums == null || nums.length == 0) {
            return null;
        }
        return builder(nums, 0, nums.length - 1);
    }

    private TreeNode builder(int[] nums, int left, int right) {
        if (left > right) {
            return null;
        }
        int mid = (left + right) / 2;
        TreeNode root = new TreeNode(nums[mid]);
        root.left = builder(nums, left, mid - 1);
        root.right = builder(nums, mid + 1, right);
        return root;
    }

    private static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }
}
